/*
 * PanelResource.java    J2RS - Jordi Sanchez
 *
 * Created on 8-dic-2011, 12:57:35
 */
package edu.uoc.tdp.client;

import edu.uoc.tdp.common.SubsystemMaintenance;
import edu.uoc.tdp.i18n.TDPLanguageUtils;
import edu.uoc.tdp.i18n.TDPLanguageConstants;
import edu.uoc.tdp.dal.ManagementResource;
import edu.uoc.tdp.entity.Resource;
import edu.uoc.tdp.customException.EIssueException;
import java.awt.Color;
import java.rmi.RemoteException;
import java.util.List;
import java.util.MissingResourceException;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import java.util.logging.Logger;
import java.util.logging.Level;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import java.util.Calendar;
import java.util.Date;
import java.util.Arrays;
import java.util.*;
import java.text.*;

/**
 *
 * @author Jordi
 */
public class PanelResource extends javax.swing.JFrame {

    //public static IManagementServer remoto;
    private final String url = new String("rmi://localhost/eEspais");
    private Boolean NewResource = false;
    public Resource ResourceTrabajo = null;
    public String UsuarioActivo = null;

    /** Creates new form PanelResource */
    public PanelResource() throws RemoteException {

        initComponents();

        /*        try {
        //remoto = new IManagementServer() {};    //RMI
        remoto = new ManagementResource(); // Sense RMI
        } catch (EIssueException ex) {
        Logger.getLogger(PanelResource.class.getName()).log(Level.SEVERE, null, ex);
        }*/

        lblFind1.setText(TDPLanguageUtils.getMessage(TDPLanguageConstants.CLIENT_PANELUSER_LBLFIND1));
        lblFindValue.setText(TDPLanguageUtils.getMessage(TDPLanguageConstants.CLIENT_PANELUSER_LBLFINDVALUE));
        lblIncactive.setText(TDPLanguageUtils.getMessage(TDPLanguageConstants.CLIENT_PANELUSER_LBLINACTIVE));

        //lblResId.setText(TDPLanguageUtils.getMessage(TDPLanguageConstants.CLIENT_PANELRESOURCE_LBLRESEID));
        lblResId.setText(TDPLanguageUtils.getMessage(TDPLanguageConstants.CLIENT_PANELSPACE_LBLSPACEID));
        lblResDesc.setText(TDPLanguageUtils.getMessage(TDPLanguageConstants.CLIENT_PANELSPACE_LBLDESCRIPTION));
//        lblAmount.setText(TDPLanguageUtils.getMessage(TDPLanguageConstants.CLIENT_PANELRESOURCE_LBLAMOUNT));
        lblPriceHour.setText(TDPLanguageUtils.getMessage(TDPLanguageConstants.CLIENT_PANELSPACE_LBLPRICEHOUR));
        lblPriceDay.setText(TDPLanguageUtils.getMessage(TDPLanguageConstants.CLIENT_PANELSPACE_LBLPRICEDAY));

        btnFind.setText(TDPLanguageUtils.getMessage(TDPLanguageConstants.CLIENT_PANELUSER_BTNFIND));
        btnNew.setText(TDPLanguageUtils.getMessage(TDPLanguageConstants.CLIENT_PANELUSER_BTNNEW));
        btnModif.setText(TDPLanguageUtils.getMessage(TDPLanguageConstants.CLIENT_PANELUSER_BTNMODIF));
        btnDelete.setText(TDPLanguageUtils.getMessage(TDPLanguageConstants.CLIENT_PANELUSER_BTNDELETE));
        btnSave.setText(TDPLanguageUtils.getMessage(TDPLanguageConstants.CLIENT_PANELUSER_BTNSAVE));
        btnCancel.setText(TDPLanguageUtils.getMessage(TDPLanguageConstants.CLIENT_PANELUSER_BTNCANCEL));

        //Deshabilitem els texts
        EnableObjects(false);
        ObjectsStartStatus();
        try {
            //Omplim Grid
            cargarGrid();
        } catch (edu.uoc.tdp.common.EIssueException ex) { //FIXME
            Logger.getLogger(PanelResource.class.getName()).log(Level.SEVERE, null, ex);
        }

        //Usuari actiu
        try {
            UsuarioActivo = Client.availableUser.getName();
            if (UsuarioActivo == "" || UsuarioActivo == null) {
                UsuarioActivo = "UNKNOWN";
            }
        } catch (Exception e) {
            Logger.getLogger(PanelUser.class.getName()).log(Level.SEVERE, null, e);
            UsuarioActivo = "UNKNOWN";
        } finally {
            lblUser.setText(UsuarioActivo);
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblResources = new javax.swing.JTable();
        lblIncactive = new javax.swing.JLabel();
        txtInactivUs = new javax.swing.JTextField();
        txtInactivDate = new javax.swing.JTextField();
        lblResId = new javax.swing.JLabel();
        lblAmount = new javax.swing.JLabel();
        txtAmount = new javax.swing.JTextField();
        txtResourceId = new javax.swing.JTextField();
        lblResDesc = new javax.swing.JLabel();
        lblPriceHour = new javax.swing.JLabel();
        txtPriceH = new javax.swing.JTextField();
        txtDescription = new javax.swing.JTextField();
        lblPriceDay = new javax.swing.JLabel();
        txtPriceD = new javax.swing.JTextField();
        lblFind1 = new javax.swing.JLabel();
        cbFind1 = new javax.swing.JComboBox();
        lblFindValue = new javax.swing.JLabel();
        txtFindValue = new javax.swing.JTextField();
        btnFind = new javax.swing.JButton();
        lblUser = new javax.swing.JLabel();
        btnNew = new javax.swing.JButton();
        btnModif = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        btnBack = new javax.swing.JButton();

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));

        tblResources.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblResources.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblResourcesMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblResources);

        lblIncactive.setText("Intactive!");

        txtInactivUs.setBackground(new java.awt.Color(255, 51, 51));
        txtInactivUs.setForeground(new java.awt.Color(255, 255, 255));

        txtInactivDate.setBackground(new java.awt.Color(255, 51, 51));
        txtInactivDate.setForeground(new java.awt.Color(255, 255, 255));

        lblResId.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblResId.setText("Id");

        lblAmount.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblAmount.setText("Amount");

        lblResDesc.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblResDesc.setText("Description");

        lblPriceHour.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblPriceHour.setText("Price/Hour");

        lblPriceDay.setText("Price/Day");

        lblFind1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblFind1.setText("Find Field");

        cbFind1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Desription", "ID" }));

        lblFindValue.setText("Value");

        btnFind.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/find.png"))); // NOI18N
        btnFind.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFindActionPerformed(evt);
            }
        });

        lblUser.setBackground(new java.awt.Color(255, 255, 255));
        lblUser.setForeground(new java.awt.Color(0, 102, 255));
        lblUser.setText("USER");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap(19, Short.MAX_VALUE)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lblResId)
                            .addComponent(lblAmount))
                        .addGap(29, 29, 29)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(txtResourceId, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtAmount, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(70, 70, 70)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblResDesc, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                .addComponent(lblPriceHour)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(27, 27, 27)
                                .addComponent(txtPriceH, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(43, 43, 43)
                                .addComponent(lblPriceDay)
                                .addGap(38, 38, 38)
                                .addComponent(txtPriceD, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(28, 28, 28)
                                .addComponent(txtDescription, javax.swing.GroupLayout.DEFAULT_SIZE, 453, Short.MAX_VALUE))))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(lblIncactive)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtInactivUs, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(txtInactivDate, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                    .addComponent(lblFind1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(cbFind1, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(34, 34, 34)
                                    .addComponent(lblFindValue)
                                    .addGap(18, 18, 18)
                                    .addComponent(txtFindValue, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(39, 39, 39)
                                    .addComponent(btnFind, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 717, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(35, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(665, Short.MAX_VALUE)
                .addComponent(lblUser, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(7, 7, 7)
                .addComponent(lblUser)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblFind1)
                            .addComponent(lblFindValue)
                            .addComponent(txtFindValue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cbFind1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(19, 19, 19))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(btnFind, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)))
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 305, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblIncactive)
                    .addComponent(txtInactivDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtInactivUs, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(58, 58, 58)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtResourceId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblResId))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtAmount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblAmount)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtDescription, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblResDesc))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtPriceH, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblPriceHour)
                            .addComponent(lblPriceDay)
                            .addComponent(txtPriceD, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(134, Short.MAX_VALUE))
        );

        btnNew.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/add.png"))); // NOI18N
        btnNew.setText("Add");
        btnNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNewActionPerformed(evt);
            }
        });

        btnModif.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/user_edit.png"))); // NOI18N
        btnModif.setText("Modif");
        btnModif.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModifActionPerformed(evt);
            }
        });

        btnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/delete.png"))); // NOI18N
        btnDelete.setText("Delete");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        btnSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/accept.png"))); // NOI18N
        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        btnCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/cancel.png"))); // NOI18N
        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        btnBack.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/door_out.png"))); // NOI18N
        btnBack.setText("Back");
        btnBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBackActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(40, 40, 40)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnBack, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE)
                    .addComponent(btnCancel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(btnModif, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnDelete, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnSave, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnNew, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(24, 24, 24))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnNew)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnModif)
                        .addGap(18, 18, 18)
                        .addComponent(btnDelete)
                        .addGap(18, 18, 18)
                        .addComponent(btnSave)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancel)
                        .addGap(420, 420, 420)
                        .addComponent(btnBack))
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(70, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /***************************************************************************
     *  Procediment per habilitar/dehabilitar objectes
     ****************************************************************************/
    private void ObjectsStartStatus() {
        //habilitem els objectes texts per a l'entrada de dadaes
        EnableObjects(false);
        //Netejem objectes de txt
        CleanTxt();
        NewResource = false;
        btnNew.setEnabled(true);
        btnModif.setEnabled(false);
        btnDelete.setEnabled(false);
        btnSave.setEnabled(false);
        btnCancel.setEnabled(false);
        btnBack.setEnabled(true);
        tblResources.setEnabled(true);
        txtInactivDate.setVisible(false);
        txtInactivUs.setVisible(false);
        lblIncactive.setVisible(false);
        txtResourceId.setBackground(Color.white);
        txtDescription.setBackground(Color.white);
        txtAmount.setBackground(Color.white);
        txtResourceId.setBackground(Color.white);
        txtPriceH.setBackground(Color.white);
        txtPriceD.setBackground(Color.white);
    }

    /***************************************************************************
     *  Procediment per habilitar/dehabilitar objectes
     ****************************************************************************/
    private void EnableObjects(Boolean EnableObj) {
        txtResourceId.setEnabled(EnableObj);
        txtDescription.setEnabled(EnableObj);
        txtAmount.setEnabled(EnableObj);
        txtPriceH.setEnabled(EnableObj);
        txtPriceD.setEnabled(EnableObj);
        //txtFindValue.setEnabled(EnableObj);

    }

    /***************************************************************************
     *   Proceciment per netejar les caselles de text
     ****************************************************************************/
    private void CleanTxt() {
        txtResourceId.setText("");
        txtDescription.setText("");
        txtAmount.setText("");
        txtPriceH.setText("");
        txtPriceD.setText("");
    }

    private void tblResourcesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblResourcesMouseClicked
        //if (tblSpaces.enable() > 0) {
        int fila = tblResources.rowAtPoint(evt.getPoint());
        int columna = tblResources.columnAtPoint(evt.getPoint());
        if ((fila > -1) && (columna > -1)) {
            try {
                //JOptionPane.showMessageDialog(this, "Space " + tblSpaces.getValueAt(fila, 0), "User", JOptionPane.INFORMATION_MESSAGE);
                String Sp = String.valueOf(tblResources.getValueAt(fila, 0));
                ShowResource(Integer.parseInt(Sp));
                //    }
            } catch (RemoteException ex) {
                Logger.getLogger(PanelResource.class.getName()).log(Level.SEVERE, null, ex);
            } catch (edu.uoc.tdp.common.EIssueException ex) {
                Logger.getLogger(PanelResource.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_tblResourcesMouseClicked

    private void btnNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNewActionPerformed
        //habilitem els objectes texts per a l'entrada de dadaes
        EnableObjects(true);
        //Netejem objectes de txt
        CleanTxt();
        NewResource = true;
        btnNew.setEnabled(false);
        btnModif.setEnabled(false);
        btnDelete.setEnabled(false);
        btnSave.setEnabled(true);
        btnCancel.setEnabled(true);
        btnBack.setEnabled(true);
        tblResources.setEnabled(false);
        NewResource=true;
    }//GEN-LAST:event_btnNewActionPerformed

    private void btnModifActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModifActionPerformed
        EnableObjects(true);
        btnModif.setEnabled(false);
        btnNew.setEnabled(false);
        btnCancel.setEnabled(true);
        btnSave.setEnabled(true);
        tblResources.setEnabled(false);
        txtResourceId.setEnabled(false);
        NewResource=false;
    }//GEN-LAST:event_btnModifActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        ObjectsStartStatus();
    }//GEN-LAST:event_btnCancelActionPerformed

    private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBackActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnBackActionPerformed

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        try {

            boolean DataOk = true;
            String TxtErr = null;

            if ((NewResource) && txtResourceId.getText().length() < 1) {
                DataOk = false;
                TxtErr = "Length ID";
                txtResourceId.setBackground(Color.red);
            }
            if (txtDescription.getText().length() < 1) {
                DataOk = false;
                TxtErr = "Description Blanc";
                txtDescription.setBackground(Color.red);
            }

            try {
                if (Double.parseDouble(txtAmount.getText()) < 0) {
                    DataOk = false;
                    TxtErr = "PriceDay < 0";
                    txtAmount.setBackground(Color.red);
                }
            } catch (NumberFormatException ex) {
                DataOk = false;
                txtAmount.setBackground(Color.red);
            }

            try {
                if (Double.parseDouble(txtPriceD.getText()) < 0) {
                    DataOk = false;
                    TxtErr = "PriceDay < 0";
                    txtPriceD.setBackground(Color.red);
                }
            } catch (NumberFormatException ex) {
                DataOk = false;
                txtPriceD.setBackground(Color.red);
            }

            try {
                if (Double.parseDouble(txtPriceH.getText()) <= 0) {
                    DataOk = false;
                    TxtErr = "PriceHour < 0";
                    txtPriceH.setBackground(Color.red);
                }
            } catch (NumberFormatException ex) {
                DataOk = false;
                txtPriceH.setBackground(Color.red);
            }


            if ((NewResource) &&  Client.remoteMaintenance.CheckExistsResource(Integer.parseInt(txtResourceId.getText()))) {
                DataOk = false;
                TxtErr = "Duplicate ID";
                txtResourceId.setBackground(Color.red);
                JOptionPane.showMessageDialog(this, "Error! Ya existe esta ID", "Error", JOptionPane.ERROR_MESSAGE);
            }

            if (DataOk) {

                Integer resourceid = Integer.parseInt(txtResourceId.getText());
                String resourcedescription = txtDescription.getText();
                Integer amount = Integer.parseInt(txtAmount.getText());
                Double price_hour = Double.parseDouble(txtPriceH.getText());
                Double price_day = Double.parseDouble(txtPriceD.getText());
                Date d1 = new Date();
                Date creation_date = new Date(d1.getDate());
                String creation_user = UsuarioActivo;

                ResourceTrabajo = new Resource(resourceid, resourcedescription, amount, price_hour, price_day, creation_date, creation_user, null, null, null);
                //Space(spaceid, spacedescription, capacity, price_hour, price_day, url_map, how_to_get, postal_code, city, space_address, creation_date, creation_user, inactivation_date, inactivation_user, record_date){
                boolean result = false;

                try {
                    if (NewResource) {
                        result = Client.remoteMaintenance.SaveResource(ResourceTrabajo);
                    } else {
                        result = Client.remoteMaintenance.SaveResourceChanges(ResourceTrabajo);
                    }
                    if (result) {
                        ObjectsStartStatus();
                        cargarGrid();
                    } else {
                        JOptionPane.showMessageDialog(this, "Error al guardar los datos", "Error", JOptionPane.ERROR_MESSAGE);
                    }
		} catch (RemoteException ex) {
            Logger.getLogger(PanelResource.class.getName()).log(Level.SEVERE, null, ex);
        } catch (edu.uoc.tdp.common.EIssueException ex) {
            Logger.getLogger(PanelResource.class.getName()).log(Level.SEVERE, null, ex);
		}   


            } else {
                JOptionPane.showMessageDialog(this, "Error! Revise los campos en rojo", "Error", JOptionPane.ERROR_MESSAGE);
            }

        } catch (RemoteException ex) {
            Logger.getLogger(PanelResource.class.getName()).log(Level.SEVERE, null, ex);
        } catch (edu.uoc.tdp.common.EIssueException ex) {
            Logger.getLogger(PanelResource.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MissingResourceException e) {
        }
    }//GEN-LAST:event_btnSaveActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        int resp = JOptionPane.showConfirmDialog(null, "Confirma eliminar?", "Eliminar", JOptionPane.YES_NO_OPTION);
        if (resp == JOptionPane.YES_OPTION) {
            try {
                InactivateResource();
            } catch (RemoteException ex) {
                Logger.getLogger(PanelResource.class.getName()).log(Level.SEVERE, null, ex);
            } catch (edu.uoc.tdp.common.EIssueException ex) {
                Logger.getLogger(PanelResource.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_btnDeleteActionPerformed

    private void btnFindActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFindActionPerformed

        //Table Usuers per busquedes
        try {
            if (txtFindValue.getText().length() == 0) {
                //Omplim Grid amb tots els
                cargarGrid();
            } else {
                Integer indx = cbFind1.getSelectedIndex();
                //Client.remoteMaintenance = new ManagementResource();
                List<Resource> ResourceList = null;
                switch (indx) {
                    case 0:
                        ResourceList = Client.remoteMaintenance.GetResource_FindByDesc(txtFindValue.getText());
                        break;
                    case 1:
                        ResourceList = Client.remoteMaintenance.GetResource_FindByResourceid(Integer.parseInt(txtFindValue.getText()));
                        break;
                }
                TableModelResource model = new TableModelResource(ResourceList);
                tblResources.setModel(model);
                tblResources.getRowSelectionAllowed();
            }
        } catch (RemoteException ex) {
            Logger.getLogger(PanelResource.class.getName()).log(Level.SEVERE, null, ex);
        } catch (edu.uoc.tdp.common.EIssueException ex) {
            Logger.getLogger(PanelResource.class.getName()).log(Level.SEVERE, null, ex);
		}     }//GEN-LAST:event_btnFindActionPerformed

    private void InactivateResource() throws RemoteException, edu.uoc.tdp.common.EIssueException {
        try {
            //Client.remoteMaintenance = new ManagementResource();
            Client.remoteMaintenance.SaveInactivationResource(Integer.parseInt(txtResourceId.getText()), UsuarioActivo);
            ObjectsStartStatus();
        } catch (RemoteException ex) {
            Logger.getLogger(PanelResource.class.getName()).log(Level.SEVERE, null, ex);
        } catch (edu.uoc.tdp.common.EIssueException ex) {
            Logger.getLogger(PanelResource.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /***************************************************************************
     *  Buscar Resources
     ****************************************************************************/
    /*
     * Carga la lista que se le env�a por par�metro en el combo del par�metro
     */
    private void cargarGrid() throws RemoteException, edu.uoc.tdp.common.EIssueException {
        //Table Usuers per busquedes
        try {
            //remoto = new ManagementResource();
            List<Resource> ResourcesList = Client.remoteMaintenance.GetAllResourcesTable();
            //set the entity objects in the list
            TableModelResource model = new TableModelResource(ResourcesList);
            tblResources.setModel(model);
            tblResources.getRowSelectionAllowed();
            tblResources.setCellSelectionEnabled(false);
        } catch (RemoteException ex) {
            Logger.getLogger(PanelResource.class.getName()).log(Level.SEVERE, null, ex);
        } catch (edu.uoc.tdp.common.EIssueException ex) {
            Logger.getLogger(PanelResource.class.getName()).log(Level.SEVERE, null, ex);

            //lblMessage.setText("Error" + ex.toString());
        }
    }

    private void ShowResource(Integer ResourceId) throws RemoteException, edu.uoc.tdp.common.EIssueException {
        //Table Usuers per busquedes
        try {

            if (ResourceId > 0) {
                //Client.remoteMaintenance = new ManagementResource();
                List<Resource> ResourceList = null;
                ResourceList = Client.remoteMaintenance.GetOneResource(ResourceId);
                Resource ResourceActiu = ResourceList.get(0);
                txtResourceId.setText(String.valueOf(String.valueOf(ResourceActiu.getResourceId())));
                txtDescription.setText(ResourceActiu.getResourceDescription().trim());
                txtAmount.setText(String.valueOf(ResourceActiu.getAmount()));
                txtPriceH.setText(String.valueOf(ResourceActiu.getPriceHour()));
                txtPriceD.setText(String.valueOf(ResourceActiu.getPriceDay()));

                //Eliminem espais del string. Ho fem aqui pq el Trim si no hi ha res dona error!?!?
                if (txtDescription.getText().length() > 0) {
                    txtDescription.setText(txtDescription.getText().trim());
                }


                String InactUser = null;
                InactUser = ResourceActiu.getInactivationUser();

                if (InactUser == null || InactUser == "") {
                    txtInactivDate.setVisible(false);
                    txtInactivUs.setVisible(false);
                    lblIncactive.setVisible(false);
                    btnModif.setEnabled(true);
                    btnDelete.setEnabled(true);
                } else {
                    DateFormat formatter;
                    formatter = new SimpleDateFormat("dd-MMM-yy");
                    txtInactivDate.setText(formatter.format(ResourceActiu.getInactivationDate()));
                    txtInactivUs.setText(ResourceActiu.getInactivationUser());
                    txtInactivDate.setVisible(true);
                    txtInactivUs.setVisible(true);
                    lblIncactive.setVisible(true);
                    btnModif.setEnabled(false);
                    btnDelete.setEnabled(false);
                }
            }
        } catch (RemoteException ex) {
            Logger.getLogger(PanelResource.class.getName()).log(Level.SEVERE, null, ex);
        } catch (edu.uoc.tdp.common.EIssueException ex) {
            Logger.getLogger(PanelResource.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PanelResource.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PanelResource.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PanelResource.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PanelResource.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    new PanelResource().setVisible(true);
                } catch (RemoteException ex) { //FIXME
                    Logger.getLogger(PanelResource.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBack;
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnFind;
    private javax.swing.JButton btnModif;
    private javax.swing.JButton btnNew;
    private javax.swing.JButton btnSave;
    private javax.swing.JComboBox cbFind1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblAmount;
    private javax.swing.JLabel lblFind1;
    private javax.swing.JLabel lblFindValue;
    private javax.swing.JLabel lblIncactive;
    private javax.swing.JLabel lblPriceDay;
    private javax.swing.JLabel lblPriceHour;
    private javax.swing.JLabel lblResDesc;
    private javax.swing.JLabel lblResId;
    private javax.swing.JLabel lblUser;
    private javax.swing.JTable tblResources;
    private javax.swing.JTextField txtAmount;
    private javax.swing.JTextField txtDescription;
    private javax.swing.JTextField txtFindValue;
    private javax.swing.JTextField txtInactivDate;
    private javax.swing.JTextField txtInactivUs;
    private javax.swing.JTextField txtPriceD;
    private javax.swing.JTextField txtPriceH;
    private javax.swing.JTextField txtResourceId;
    // End of variables declaration//GEN-END:variables
}

class TableModelResource extends AbstractTableModel {

    List ResourcesList;
    String c1 = TDPLanguageUtils.getMessage(TDPLanguageConstants.CLIENT_PANELRESOURCE_TABLEC1);
    String c2 = TDPLanguageUtils.getMessage(TDPLanguageConstants.CLIENT_PANELRESOURCE_TABLEC2);
    String c3 = TDPLanguageUtils.getMessage(TDPLanguageConstants.CLIENT_PANELRESOURCE_TABLEC3);
    String c4 = TDPLanguageUtils.getMessage(TDPLanguageConstants.CLIENT_PANELRESOURCE_TABLEC4);
    String c5 = TDPLanguageUtils.getMessage(TDPLanguageConstants.CLIENT_PANELRESOURCE_TABLEC5);
//    String headerList[] = new String[]{c1, c2, c3, c4, c5};
    String headerList[] = new String[]{"ID", "Description", "Amount", "price/Hour", "price/Day"};

    public TableModelResource(List list) {
        ResourcesList = list;
    }

    @Override
    public int getColumnCount() {
        return 5;
    }

    @Override
    public int getRowCount() {
        return ResourcesList.size();
    }
    // this method is called to set the value of each cell

    @Override
    public Object getValueAt(int row, int col) {
        Object returnValue = false;
        //User entity = null;
        Resource entity = (Resource) ResourcesList.get(row);
        switch (col) {
            case 0:
                returnValue = entity.getResourceId();
                break;
            case 1:
                returnValue = entity.getResourceDescription();
                break;
            case 2:
                returnValue = entity.getAmount();
                break;
            case 3:
                returnValue = entity.getPriceHour();
                break;
            case 4:
                returnValue = entity.getPriceDay();
                break;
        }
        return returnValue;
    }
    //display the name of columns

    @Override
    public String getColumnName(int col) {
        return headerList[col];
    }
}